package com.maimang.platform.dsp.controller;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.dsp.entity.Advertiser;
import com.maimang.platform.dsp.entity.AdvertiserData;
import com.maimang.platform.dsp.entity.AdvertiserShort;
import com.maimang.platform.dsp.service.AdvertiserDataService;
import com.maimang.platform.dsp.service.AdvertiserService;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname AdvertiserDataController
 * @description <br>广告主数据信息操作处理</br>
 * @date 2019-08-13 15:35:11
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/dsp/data/advertiser")
@Api(value = "广告主数据", description = "广告主数据信息操作处理接口")
public class AdvertiserDataController extends BaseController {
	private String prefix = "dsp/data/advertiser";

	@Autowired
	private AdvertiserDataService advertiserDataService;
	@Autowired
	private AdvertiserService advertiserService;

	@RequiresPermissions("dsp:data:view")
	@GetMapping()
	public String advertiserData() {
		return prefix + "/data";
	}

	/**
	 * 查询广告主数据列表
	 */
	@ApiOperation(value = "查询列表", notes = "查询广告主数据列表信息")
	@RequiresPermissions("dsp:data:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(AdvertiserData advertiserData) {
		startPage();
		List<AdvertiserData> list = advertiserDataService.selectAdvertiserDataList(advertiserData);
		return getDataTable(list);
	}

	/**
	 * 查询广告主列表
	 */
	@ApiOperation(value = "查询列表", notes = "查询广告主列表信息")
	@RequiresPermissions("dsp:advertiser:list")
	@PostMapping("/dataList")
	@ResponseBody
	public TableDataInfo dataList(Advertiser advertiser) {
		startPage();
		List<AdvertiserShort> list = advertiserService.selectShortList(advertiser);
		return getDataTable(list);
	}

	/**
	 * 导出广告主数据列表
	 */
	@ApiOperation(value = "导出列表", notes = "导出广告主数据列表信息到Excel文件")
	@RequiresPermissions("dsp:advertiser:export")
	@Log(title = "广告主信息", businessType = BusinessType.INSERT)
	@PostMapping("/exportAdvertiser")
	@ResponseBody
	public Result exportAdvertiser(Advertiser advertiser) {
		List<AdvertiserShort> list = advertiserService.selectShortList(advertiser);
		ExcelUtil<AdvertiserShort> util = new ExcelUtil<AdvertiserShort>(AdvertiserShort.class);
		return util.exportExcel(list, "广告主信息");
	}

	/**
	 * 导出广告主数据列表
	 */
	@ApiOperation(value = "导出列表", notes = "导出广告主数据列表信息到Excel文件")
	@RequiresPermissions("dsp:data:export")
	@Log(title = "广告主数据", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public Result export(AdvertiserData advertiserData) {
		List<AdvertiserData> list = advertiserDataService.selectAdvertiserDataList(advertiserData);
		ExcelUtil<AdvertiserData> util = new ExcelUtil<AdvertiserData>(AdvertiserData.class);
		return util.exportExcel(list, "广告主数据");
	}

	/**
	 * 查询广告主数据详细
	 */
	@RequiresPermissions("dsp:advertiser:list")
	@GetMapping("/detail/{advertiserId}")
	public String detail(@PathVariable("advertiserId") String advertiserId, ModelMap modelMap) {
		modelMap.put("advertiserId", advertiserId);
		Advertiser advertiser = advertiserService.selectAdvertiserById(Long.parseLong(advertiserId));
		modelMap.put("name", advertiser.getName());
		return "dsp/data/advertiser/detail";
	}

	/**
	 * 新增广告主数据
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存广告主数据
	 */
	@ApiOperation(value = "新增保存", notes = "参数：AdvertiserData")
	@RequiresPermissions("dsp:data:add")
	@Log(title = "广告主数据", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(AdvertiserData advertiserData) {
		return toResult(advertiserDataService.insertAdvertiserData(advertiserData));
	}

	/**
	 * 修改广告主数据
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap modelMap) {
		AdvertiserData advertiserData = advertiserDataService.selectAdvertiserDataById(id);
		modelMap.put("advertiserData", advertiserData);
		return prefix + "/edit";
	}

	/**
	 * 同步广告主数据
	 */
	@ApiOperation(value = "同步数据", notes = "参数：AdvertiserData")
	@RequiresPermissions("dsp:data:add")
	@Log(title = "广告主数据同步", businessType = BusinessType.IMPORT)
	@PostMapping("/sync")
	@ResponseBody
	public Result sync(String ids) {
		//
		return toResult(advertiserDataService.syncAdvertiserDataByIds(ids, 30));
	}

	/**
	 * 修改保存广告主数据
	 */
	@ApiOperation(value = "修改保存", notes = "参数：AdvertiserData")
	@RequiresPermissions("dsp:data:edit")
	@Log(title = "广告主数据", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(AdvertiserData advertiserData) {
		return toResult(advertiserDataService.updateAdvertiserData(advertiserData));
	}

	/**
	 * 删除广告主数据
	 */
	@ApiOperation(value = "删除对象", notes = "参数：ids，示例： id1,id2,id3，接口会转转为String[]类型")
	@RequiresPermissions("dsp:data:remove")
	@Log(title = "广告主数据", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(advertiserDataService.deleteAdvertiserDataByIds(ids));
	}


	@ApiOperation(value = "清空记录", notes = "清空广告主所有数据")
	@RequiresPermissions("dsp:data:remove")
	@Log(title = "广告主数据", businessType = BusinessType.DELETE)
	@PostMapping("/removeData")
	@ResponseBody
	public Result removeAll(String advertiserId) {
		return toResult(advertiserDataService.deleteByAdvertiserId(advertiserId));
	}
}